angular.module('detail')
	.controller("productCtrl",function($scope,$http){
		//初始化数据
		var getallRistoryReq={
			url:"/Warehouse/ristories",
			method:"GET"
		}
		$http(getallRistoryReq).then(function (res) {
			$scope.ristories = res.data;
		},function () {
			alert("连接后台失败");
		});
		var getallProductReq={
			url:"/Warehouse/products",
			method:"GET"
		}
		$http(getallProductReq).then(function (res) {
			$scope.productsInView = res.data;
		},function () {
			alert("连接后台失败");
		})
		
		//根据ristory展示产品
		$scope.currentRistory = -1;
		$scope.showProducts = function(index){
			var getProductsByRidReq = {
				url:"/Warehouse/products?rid=" +$scope.ristories[index]._id,
				method:"GET"
			}
			$http(getProductsByRidReq).then(function (res) {
				$scope.productsInView = res.data;
				$scope.currentRistory = index;
			},function () {
				alert("连接后台失败");
			})
		}

		//增加产品
		$scope.pname = "";
		$scope.price = "";
		$scope.num = "";
		$scope.addProduct = function(){
			var addPReq = {
				url:"/Warehouse/product",
				method:"POST",
				data:{pname:$scope.pname,price:$scope.price,num:$scope.num,rid:$scope.ristories[$scope.currentRistory]._id},
				headers:{
					"Content-type":"application/json"
				}
			}
			$http(addPReq).then(function (res) {	
				$scope.productsInView.push(res.data)
			},function () {
				alert("连接后台失败");
			})
		}
		//删除产品
		$scope.deleteProduct = function(index){
			var deletePReq = {
					url:"/Warehouse/product/" + $scope.productsInView[index]._id,
					method:"DELETE",
				}
				$http(deletePReq).then(function (res) {	
					$scope.productsInView.splice(index,1);
				},function () {
					alert("连接后台失败");
				})
		}
		//修改产品
		var currentProduct = 0;
		$scope.alterProduct = function(index){
			currentProduct = index;
			$scope.alterId = $scope.productsInView[index]._id;
			$scope.pname = $scope.productsInView[index].pname;
			$scope.price = $scope.productsInView[index].price;
			$scope.num = $scope.productsInView[index].num;
		}
		
		$scope.submitAlter = function(){
			var putReq={
				url:"/Warehouse/product/"+ $scope.alterId,
				method:"PUT",
				data:{pname:$scope.pname,price:$scope.price,num:$scope.num},
				headers:{
					"Content-type":"application/json"
				}
			}
			$http(putReq).then(function (res) {	
				$scope.productsInView[currentProduct] = res.data;
			},function () {
				alert("连接后台失败");
			})
		}
	})
	.controller("ristoryCtrl",function($scope,$http, $location){
		$scope.rname = "";
		$scope.address = "";

		$scope.addRistory = function () {
			var addRistoryReq = {
					url: "/Warehouse/ristory",
					method:"POST",
					data:{rname:$scope.rname,address:$scope.address},
					headers:{
						"Content-type":"application/json"
					}
				}
			$http(addRistoryReq).then(function (res) {
				$location.path("/product");
			},function () {
				alert("连接后台失败");
			})
		}
	})
	.controller("loginCtrl",function($scope,$http,$location,$rootScope){
		$scope.username = "";
		$scope.password = "";
		$scope.login = function () {
			var loginReq = {
				url:"/Warehouse/user?username=" + $scope.username+"&password=" + $scope.password,
				method:"GET"
			}
			$http(loginReq).then(function(res){
				$rootScope.current_user = res.username;
				 $location.path("/product");
			},function (res) {
				alert("登录失败");
			})
		}
	})
	.controller("signupCtrl",function($scope,$http,$location,$rootScope){
		$scope.username = "";
		$scope.password = "";
		$scope.signup = function () {
			var signupReq = {
				url:"/Warehouse/user",
				method:"POST",
				data:{username:$scope.username,password:$scope.password},
				headers:{
					"Content-type":"application/json"
				}
			}
			$http(signupReq).then(function(res){
				$rootScope.current_user = res.username;
				$location.path("/product");
			},function (res) {
				alert("注册失败");
			})
		}
	})
	.controller("navCtrl",function($rootScope,$http,$location){
		$rootScope.current_user = '';
		$rootScope.logOut = function() {
			var logOutReq = {
				url:"/Warehouse/user/logOut",
				method:"DELETE"
			}
			$http(logOutReq).then(function (res) {
				$rootScope.current_user = ''
				$location.path("/login");
			},function () {
				alert("连接后台失败");
			});
		};
	})
